package com.maxima.attendance.actions;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;

import com.maxima.attendance.constants.Constants;
import com.maxima.attendance.daos.HolidayDAO;
import com.maxima.attendance.daos.ProjectDAO;
import com.maxima.attendance.daos.TimeLogDAO;
import com.maxima.attendance.daos.UserDAO;
import com.maxima.attendance.pojos.Employee;
import com.maxima.attendance.pojos.Holiday;
import com.maxima.attendance.pojos.Project;
import com.maxima.attendance.pojos.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InitializeList extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private Map<String, Object> map = ActionContext.getContext().getSession();
	private List<Employee> employeeList;
	private List<Project> projectList;
	private List<Holiday> holidayList;
	private List<Employee> workFromHomeEmployeeList;
	private String dateString;
	private Integer projectId;
	private String jsonResponse = "";
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	private UserDAO userDAO = new UserDAO();
	private ProjectDAO projectDAO = new ProjectDAO();
	private HolidayDAO holidayDAO = new HolidayDAO();
	private TimeLogDAO timeLogDAO = new TimeLogDAO();

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}

	@Action(value = "refreshUsers", results = {
	@Result(name = "user", type = "tiles", location = "userHome.tiles"), @Result(name = "admin", type = "tiles", location = "adminUserStatus.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "defaultInterceptorStack")
	})
	public String initActiveEmployees() {
		setEmployeeList(getActiveEmployees());
		return SUCCESS;
	}

	@Action(value = "showUserScreen", results = {
	@Result(name = "success", type = "tiles", location = "showUser.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initAllEmployees() {
		initLists();
		return SUCCESS;
	}

	@Action(value = "showAddUserScreen", results = {
	@Result(name = "success", type = "tiles", location = "showAddUser.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initAllEmployeesForAdd() {
		initLists();
		return SUCCESS;
	}

	@Action(value = "showAddHolidayScreen", results = {
	@Result(name = "success", type = "tiles", location = "addNewHoliday.tiles"), @Result(name = "error", location = "index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initAllEmployeesForAddHolidays() {
		initLists();
		return SUCCESS;
	}
	
	private void initLists() {
		setEmployeeList(getAllEmployees());
		setProjectList(initProjectList());
		setHolidayList(initHolidayList());
	}

	@Action(value = "showProjectsScreen", results = {
	@Result(name = "success", type = "tiles", location = "showProject.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initProjects() {
		setProjectList(initProjectList());
		return SUCCESS;
	}

	@Action(value = "showWorkFromHomeScreen", results = {
	@Result(name = "success", type = "tiles", location = "showWorkFromHome.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initWorkFromHome() {
		setProjectList(initProjectList());
		return SUCCESS;
	}

	@Action(value = "showHolidaysScreen", results = {
	@Result(name = "success", type = "tiles", location = "showHolidays.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initHolidays() {
		setProjectList(initProjectList());
		setHolidayList(initHolidayList());
		return SUCCESS;
	}

	@Action(value = "initWorkFromHomeScreen", results = {
	@Result(name = "success", type = "tiles", location = "showWorkFromHome.tiles"), @Result(name = "error", location = "/index.jsp")
	}, interceptorRefs = {
		@InterceptorRef(value = "adminStack")
	})
	public String initWorkFromHomeEmployees() {
		try {
			initWorkFromHomeScreen();
			return SUCCESS;
		}
		catch (NumberFormatException e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	private void initWorkFromHomeScreen() {
		setDateString(ServletActionContext.getRequest().getParameter("date"));
		setProjectId(Integer.parseInt(ServletActionContext.getRequest().getParameter("project")));
		setWorkFromHomeEmployeeList(initWorkFromHomeEmployees(getDate(getDateString()), getProjectId()));
		setProjectList(initProjectList());
		map.put(Constants.SELECTED_DATE, getDateString());
	}

	private List<Employee> initWorkFromHomeEmployees(Date date, Integer projectId) {
		return getEmployeeList(date, userDAO.getAllUsersOfSameProject(projectId));
	}

	private Date getDate(String dateString) {
		Date date = null;
		if (dateString != null) {
			try {
				date = sdf.parse(dateString);
			}
			catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return date;
	}

	private List<Employee> getActiveEmployees() {
		return getEmployees(userDAO.getAllActiveUsers());
	}

	private List<Employee> getAllEmployees() {
		return getEmployees(userDAO.getAllUsers());
	}

	private List<Employee> getEmployees(List<User> usersList) {
		return getEmployeeList(new Date(), usersList);
	}

	private List<Employee> getEmployeeList(Date date, List<User> usersList) {
		List<Employee> employees = new ArrayList<Employee>();
		for (User user : usersList) {
			employees.add(new Employee(user, timeLogDAO.getTimeLogByDate(user.getId(), date)));
		}
		return employees;
	}

	public List<Project> initProjectList() {
		return projectDAO.getAll();
	}

	private List<Holiday> initHolidayList() {
		return holidayDAO.getAllHolidays();
	}

	public List<Project> getProjectList() {
		return projectList;
	}

	public void setProjectList(List<Project> projectList) {
		this.projectList = projectList;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	public List<Holiday> getHolidayList() {
		return holidayList;
	}

	public void setHolidayList(List<Holiday> holidayList) {
		this.holidayList = holidayList;
	}

	public List<Employee> getWorkFromHomeEmployeeList() {
		return workFromHomeEmployeeList;
	}

	public void setWorkFromHomeEmployeeList(List<Employee> workFromHomeEmployeeList) {
		this.workFromHomeEmployeeList = workFromHomeEmployeeList;
	}

	public String getDateString() {
		return dateString;
	}

	public void setDateString(String dateString) {
		this.dateString = dateString;
	}

	public Integer getProjectId() {
		return projectId;
	}

	public void setProjectId(Integer projectId) {
		this.projectId = projectId;
	}

	public String getJsonResponse() {
		return jsonResponse;
	}

	public void setJsonResponse(String jsonResponse) {
		this.jsonResponse = jsonResponse;
	}
}